package com.smarthealthring.mm.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;

import java.io.ByteArrayOutputStream;

/**
 * 处理bitmap的工具类
 *
 * Created by hewei26 on 2016/11/4.
 */

public class BitmapUtils {
    public static final int IMAGE_MAXSIZE = 32768;//微信分享图片大小限制

    /**
     * 每次压缩质量 -10  压缩完回收
     */
    public static Bitmap compressImage(final Bitmap bmp) {
        //压缩图片每次质量-10,  直到图片在内存里的开销小于32K
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
        int options = 100;
        while (baos.toByteArray().length > IMAGE_MAXSIZE && options != 10) {
            baos.reset(); //清空baos
            bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%，把压缩后的数据存放到baos中
            options -= 10;
        }

        byte[] result = baos.toByteArray();
        try {
            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
        return bitmap;
    }

    /**
     * 图片按比例大小压缩方法
     * @param srcPath （根据路径获取图片并压缩）
     * @return
     */
    public static Bitmap getSmallImage(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        // 开始读入图片，此时把options.inJustDecodeBounds 设回true了
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        float hh = 100f;
        float ww = 100f;
        // 缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = 1;// be=1表示不缩放
        if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0)
            be = 1;
        newOpts.inSampleSize = be;// 设置缩放比例
        // 重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩
    }

    public static Bitmap getSnapBitmap(View view) {
        return getSnapBitmap(view, Bitmap.Config.ARGB_8888);
    }

    public static Bitmap getSnapBitmap(View view, Bitmap.Config config) {
        try {
            Bitmap snapcache;
            snapcache = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), config);
            Canvas c = new Canvas(snapcache);
            c.drawColor(Color.WHITE);
            view.draw(c);
            return snapcache;
        } catch (OutOfMemoryError out) {
            out.printStackTrace();
            gc();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            gc();
        }
        return createDefaultBitmap();
    }

    private static void gc() {
        System.gc();
    }

    private static Bitmap createDefaultBitmap() {
        Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        return bitmap;
    }


}
